package com.xms.schema.api.controller;

import com.xms.dependency.XmsDependencyException;
import com.xms.schema.api.model.DeleteEntityModel;
import com.xms.schema.service.entity.IEntityDeleterService;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.Arrays;

/**
 * <p>
 * EntityDeleterController
 * </p>
 *
 * @author migo
 * @since 2021/9/18
 */
@RestController
@RequestMapping("{org}/api/schema/entity")
public class EntityDeleterController extends ApiBaseController {
    private IEntityDeleterService _entityDeleterService;
    @Autowired
    public void EntityDeleterController(IEntityDeleterService entityDeleterService) {
        _entityDeleterService = entityDeleterService;
    }

    @ApiOperation("删除实体")
    @PostMapping("deletebyid")
    public JsonResultObject deleteById(@RequestBody DeleteEntityModel model) throws SQLException, XmsDependencyException {
        if(CollectionUtil.isEmpty(model.RecordId)){
            return JNotSpecifiedParams("id");
        }
        model.deletetable = (model.deletetable == null ? true : model.deletetable);
        if(_entityDeleterService.deleteByIds(Arrays.asList(model.RecordId), model.deletetable)){
            return JDeleteSuccess();
        }
        return JDeleteFailure();
    }
}
